﻿Imports Negocio
Imports Servicios
Imports Excepciones

Public Class frmConsultaActividades

    Private _tarea As New Tarea()

    Protected Overrides Sub PreLoad()
        Me.Icon = Icon.FromHandle(Global.SIPRO.My.Resources.Resources.activity.GetHicon())
    End Sub

    Sub New(tarea As Tarea)
        InitializeComponent()
        _tarea = tarea
    End Sub
    Private Sub frmConsultaActividades_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        dtFecha.Value = Nothing

        Try
            cbUsuarios.FirstBlankItem = True
            cbUsuarios.DataSource = GesUsuarios.Consultar(Nothing, True)

        Catch ex As Exception
            My.Application.HandlerException(ex)
            Me.Close()
        End Try
        Search()
    End Sub

    Sub Search()

        Try
            Dim usu As New Usuario()
            usu.OID = Nothing
            If TypeOf cbUsuarios.SelectedItem Is Usuario Then
                usu = DirectCast(cbUsuarios.SelectedItem, Usuario)
            End If


            dgvActividades.DataSource = GesActividades.Consultar(_tarea, dtFecha.Value, usu)

        Catch ex As Exception
            My.Application.HandlerException(ex)
            Me.Close()
        End Try
    End Sub

    Private Sub btnBuscar_Click(sender As System.Object, e As System.EventArgs) Handles btnBuscar.Click
        Search()
    End Sub

    Private Sub btnCerrar_Click(sender As System.Object, e As System.EventArgs) Handles btnCerrar.Click
        Me.Close()
    End Sub

    Private Sub dgvActividades_DoubleClick(sender As System.Object, e As System.EventArgs) Handles dgvActividades.DoubleClick
        If dgvActividades.Rows.Count > 0 AndAlso TypeOf dgvActividades.CurrentRow.DataBoundItem Is Actividad Then
            Dim Act As Actividad = DirectCast(dgvActividades.CurrentRow.DataBoundItem, Actividad)
            Dim f As New frmABMActividad(Act, _tarea)
            If f.ShowDialog = DialogResult.OK Then
                Search()
            End If
        Else
            My.Application.HandlerException(New ItemSelectRequiredException())
        End If
    End Sub

    Private Sub btnNuevo_Click(sender As System.Object, e As System.EventArgs) Handles btnNuevo.Click
        Try
            Dim f = New frmABMActividad(_tarea)
            If f.ShowDialog() = Windows.Forms.DialogResult.OK Then
                Search()
            End If
        Catch ex As Exception

        End Try

    End Sub

    Private Sub btnEliminar_Click(sender As System.Object, e As System.EventArgs) Handles btnEliminar.Click
        If dgvActividades.Rows.Count > 0 AndAlso TypeOf dgvActividades.CurrentRow.DataBoundItem Is Actividad Then
            If MessageExt.Show(GesIdiomas.TraduceMensaje("MSGACT001").ToString, MessageBoxButtons.OKCancel) = DialogResult.OK Then
                Dim Act As Actividad = DirectCast(dgvActividades.CurrentRow.DataBoundItem, Actividad)
                Try
                    GesActividades.Eliminar(Act)
                    Search()
                Catch ex As Exception
                    My.Application.HandlerException(ex)
                End Try
            End If
        Else
            My.Application.HandlerException(New ItemSelectRequiredException())
        End If
    End Sub

End Class